class User_StdDev_Filter(object):
  def __init__(self, tr, data, range):
    self.tr, self.data, self.range = tr, data, range
  def rectify(self, r, mid, uid):
    mean, stddev = self.data[int(uid)]
    if len(self.tr.user(uid)) > 10:
      return min(max(r, mean - stddev * self.range), mean + stddev * self.range)
    else:
      return r

class Movie_StdDev_Filter(object):
  def __init__(self, tr, data, range):
    self.tr, self.data, self.range = tr, data, range
  def rectify(self, r, mid, uid):
    mean, stddev = self.data[int(mid)]
    if len(self.tr.movie(mid)) > 10:
      return min(max(r, mean - stddev * self.range), mean + stddev * self.range)
    else:
      return r